package com.shycloud.mido.homework.thread;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shycloud.mido.homework.api.entity.WeekMain;
import com.shycloud.mido.homework.api.vo.WeekPayloadVO;
import com.shycloud.mido.homework.constant.Constant;
import com.shycloud.mido.homework.mapper.WeekMainMapper;
import com.shycloud.mido.homework.service.MidoHomeworkWeekService;

import lombok.extern.slf4j.Slf4j;
 
@Component
@Slf4j
public class WeekSendAsyncTask {
	/** 周报服务 */
	@Autowired
	private MidoHomeworkWeekService midoHomeworkWeekService;
	@Autowired
	private WeekMainMapper weekMainMapper;
	
	@Async
    public void asyncExecute(LocalDateTime now) throws Exception{
		StopWatch stopWatchMain = new StopWatch();
		stopWatchMain.start();
		if (Objects.isNull(now)) {
			now  = LocalDateTime.now();
		}
		WeekPayloadVO payload = new WeekPayloadVO();
		payload.setNow(now);
		try {
			log.info("weekSendMember 周报执行时间: {} ", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(now));
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			//开始和结束 存在就查询出来
		    LambdaQueryWrapper<WeekMain> mainQW = Wrappers.<WeekMain>lambdaQuery()
		    		.eq(WeekMain::getStatus, Constant.WEEK_MAIN_STATUS_CALCSUCCESS)
	        		.eq(WeekMain::getStep, Constant.WEEK_MAIN_STEP_EXEC_DONE)
	        		.orderByDesc(WeekMain::getCreateTime)
	                .last("limit 1");
		    WeekMain weekMain = weekMainMapper.selectOne(mainQW);
		    if (Objects.nonNull(weekMain)) {
				payload.setWeekMain(weekMain);
				payload.setWeekMainId(weekMain.getId());
				LocalDate startWeekDate = weekMain.getStartWeekDate();
				LocalDate endWeekDate = weekMain.getEndWeekDate();
			    LocalDateTime startTime = startWeekDate.atTime(0, 0, 0, 0);
				LocalDateTime endTime = endWeekDate.atTime(23, 59, 59);
				payload.setStartTime(startTime);
				payload.setEndTime(endTime);
				log.info("weekSendMember start: {}", payload);
				midoHomeworkWeekService.weekSendMember(payload);
				log.info("weekSendMember end: {}", payload);
			}
		    //更新步骤
			if (StringUtils.isNotBlank(payload.getWeekMainId())) {
				LambdaUpdateWrapper<WeekMain> weekMailUW = Wrappers.<WeekMain>lambdaUpdate()
						.set(WeekMain::getStep, Constant.WEEK_MAIN_STEP_SEND_DONE)
						.set(WeekMain::getStatus, Constant.WEEK_MAIN_STATUS_CALCSUCCESS)
						.set(WeekMain::getGenEndtime, LocalDateTime.now())
						.eq(WeekMain::getId, payload.getWeekMainId());
				weekMainMapper.update(null, weekMailUW);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			//更新步骤
			if (StringUtils.isNotBlank(payload.getWeekMainId())) {
				LambdaUpdateWrapper<WeekMain> weekMailUW = Wrappers.<WeekMain>lambdaUpdate()
						.set(WeekMain::getStep, Constant.WEEK_MAIN_STEP_FAIL)
						.set(WeekMain::getStatus, Constant.WEEK_MAIN_STATUS_CALCERROR)
						.eq(WeekMain::getId, payload.getWeekMainId());
				weekMainMapper.update(null, weekMailUW);
			}
		}
		stopWatchMain.stop();
		log.info("asyncExecute耗时：{}", (double) stopWatchMain.getTime() / 1000 + "s");
    }
    
    
  
        
}